<?php

class LinkAction extends Action 
{

    public function index()
    {
		$Link=D("Link");
		$count=$Link->count();
		import("ORG.Util.Page");
		$listRows=10;
		$p=new page($count,$listRows);
		$list=$Link->findAll(null,'*','orderid desc,addtime desc',$p->firstRow.','.$p->listRows);
		$page=$p->show();
		if ($list!==false) 
		{
			$this->assign('page',$page);
			$this->assign('list',$list);
		}
		$this->display();
    }
    public function add()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
    		$title=isset($_POST['title'])?trim($_POST['title']):'';
    		$url=isset($_POST['url'])?trim($_POST['url']):'';
    		$type=isset($_POST['type'])?(int)($_POST['type']):0;
    		$orderid=isset($_POST['orderid'])?trim($_POST['orderid']):0;
    		
    		if($title=='')
    		{
    			$msg="链接标题不能为空";$this->assign('jumpUrl',__URL__);
    			$this->error($msg);return; 
    		}	
    		if($url=='')
    		{
    			$msg="链接地址不能为空";$this->assign('jumpUrl',__URL__);
    			$this->error($msg);return; 
    		}	
			
			if($type==1)
			{
	        	import("ORG.Net.UploadFile");
	        	$upload = new UploadFile();     
    	 	    $upload->maxSize  = 500*1024 ;   
     		    $upload->thumb=false;    
     		    $upload->autoSub=true;
	     	    $upload->subType='date';
    	 	    $upload->dateFormat="Ymd";    
    	 	    $upload->saveRule="uniqid";   	    
        		$upload->allowExts  = explode(',','png,jpg,jpeg,gif');
	        	$upload->savePath = C('MYUPLOAD_PATH').'/link/'; 
      
        		if(!$upload->upload()) {
	                $this->assign('jumpUrl',__URL__);
    	            $this->error($upload->getErrorMsg());return;            	
        	    }
	            $ret=$upload->getUploadFileInfo();
				$logo=C('MYUPLOAD_PATH').'/link/'.$ret[0]['savename'];				    		
			}
			
			$Link=D("Link");
			if($orderid==0)
			{
				$orderid=$Link->max('orderid');
				$orderid=$orderid+1;
			}
    		$data=array(
    			'title'=>$title,
    			'url'=>$url,    
    			'type'=>$type,			
    			'orderid'=>$orderid,
    		);    		
    		if($type==1)$data['logo']=$logo;    		
    		if(!$Link->create($data)){
    			$this->error($Link->getError());return;
    		}
    		if(!$Link->add()){ 		
    			$this->error(L('_INSERT_FAIL_'));return;
    		}
			$this->cache();    		
    		$msg="友情链接添加成功";
    		$this->assign('jumpUrl',__URL__);
    		$this->success($msg);
    	}
    }
    public function edit()
    {
    	if(!$this->isPost())
    	{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			if (!$id){
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$Link=D("Link");
			$list=$Link->find($id);
			if (!$list){
					$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$this->assign('vo',$list);
			$this->display();			    			
    	}
    	else
    	{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$temp=D('Link')->find($id);
			if ($id==0 || !$temp){ 
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$oldlogo=$temp['logo'];
    		$title=isset($_POST['title'])?trim($_POST['title']):'';
    		$url=isset($_POST['url'])?trim($_POST['url']):'';
    		$type=isset($_POST['type'])?(int)($_POST['type']):0;    		
    		$orderid=isset($_POST['orderid'])?trim($_POST['orderid']):0;

    		if($title=='')
    		{
    			$msg="链接标题不能为空";$this->assign('jumpUrl',__URL__);
    			$this->error($msg);return; 
    		}	    		
    		if($url=='')
    		{
    			$msg="链接地址不能为空";$this->assign('jumpUrl',__URL__);
    			$this->error($msg);return; 
    		}  		
    		$data=array(
    			'id'=>$id,
				'title'=>$title,
    			'url'=>$url,
    			'type'=>$type,
    			'orderid'=>$orderid,
    		);
    		if($_FILES['logo']['name']!='' && $type==1)
    		{
	        	import("ORG.Net.UploadFile");
	        	$upload = new UploadFile();     
    	 	    $upload->maxSize  = 200*1024 ;   
     		    $upload->thumb=true;
	     	    $upload->thumbMaxHeight=40;
    	 	    $upload->thumbMaxWidth=160;     	    
     		    $upload->autoSub=true;
	     	    $upload->subType='date';
    	 	    $upload->dateFormat="Ymd";     	    
        		$upload->allowExts  = explode(',','png,jpg,jpeg,bmp,gif');
	        	$upload->savePath = C('MYUPLOAD_PATH').'/link/'; 
      
        		if(!$upload->upload()) {
	                $this->assign('jumpUrl',__URL__);
    	            $this->error($upload->getErrorMsg());return;            	
        	    }
	            $ret=$upload->getUploadFileInfo();
				$logo=C('MYUPLOAD_PATH').'/link/'.$ret[0]['savename'];
				if(@file_exists($oldlogo))@unlink($oldlogo);			
    		}	
    		if(isset($logo))$data['logo']=$logo;		
			$Link=D("Link");
			if(!$Link->create($data)){
				$this->error($Link->getError());return;
			}
			if(!$Link->save()){
				$this->error(L('_UPDATE_FAIL_'));return;
			}
			$this->cache();			
			$this->assign('jumpUrl',__URL__);
			$msg="友情链接修改成功";
			$this->success($msg);		
    	}    	
    }
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id)
		{
			$this->error(L('_SELECT_NOT_EXIST_'));return;
		}
		$Link=D("Link");
		$temp=$Link->find($id);
		$type=$temp['type'];
		if (!$temp)
		{
			$this->error(L('_SELECT_NOT_EXIST_'));return;
		}
		if(!$Link->deleteById($id))
		{
			$this->error(L('_DELETE_FAIL_'));return;
		}		
		if($type==1)
		{
			if(@file_exists($temp['logo']))@unlink($temp['logo']);
		}
		$this->cache();
		$this->redirect();
	}	
	private function cache()
	{
		$Link=D("Link");
		$condition['type']=1;
		$list=$Link->findAll($condition,'title,url,logo','orderid desc,addtime desc');
		$filename=C('MYCACHE_PATH').'/linklogo.php';
		if(@file_exists($filename)){ @unlink($filename);}
    	$content="<?php\n \$linklogo=".var_export($list,true)."\n?>";    	    	
    	@!file_put_contents($filename,$content);	
    	
		$condition=array();
		$condition['type']=0;
		$list=$Link->findAll($condition,'title,url','orderid asc');
		$filename=C('MYCACHE_PATH').'/linktext.php';
		if(@file_exists($filename)){ @unlink($filename);}
    	$content="<?php\n \$linktext=".var_export($list,true)."\n?>";    	    	
    	@!file_put_contents($filename,$content);	    	
	}
}
?>